const Controller = require('egg').Controller;

class BlogsController extends Controller {
  async add() {
    const { app, ctx } = this;
    // 验证参数
    try {
      ctx.validate({
        title: {
          type: 'string',
          require: true
        },
        content: 'string',
        author: 'string'
      }, ctx.request.body);
    } catch (error) {
      ctx.status = 400;
      ctx.body = { code: 0, msg: '参数传递有误', error: error.errors[0] }
      return;
    }
    const data = await this.ctx.service.blogs.add()
    ctx.body = data
  }
  async delete() {
    const { app, ctx } = this;
    // try {
    //   ctx.validate({ id }, ctx.query);
    // } catch (error) {
    //   ctx.status = 400;
    //   ctx.body = { code: 0, msg: '参数传递有误', error: error.errors[0] }
    //   return;
    // }
    const data = await this.ctx.service.blogs.delete()
    ctx.body = data
  }
  async deleteAll() {
    const data = await this.ctx.service.blogs.deleteAll()
    this.ctx.body = data
  }
  async update() {
    const data = await this.ctx.service.blogs.update()
    this.ctx.body = data
  }
  async findAll() {
    const { app, ctx } = this;
    // console.log('app:------>', app)
    // // console.log('ctx:------>', ctx)
    // console.log('serviceClasses:', app.serviceClasses.blogs)
    const data = await this.ctx.service.blogs.findAll()
    this.ctx.body = data
  }
  async search() {
    const { app } = this;
    ctx.body = 'add'
  }
  async page() {
    const data = await this.ctx.service.blogs.page()
    this.ctx.body = data
  }
}

module.exports = BlogsController;